/*This module contains all the APIs which will send ajax request*/
import ajax from './ajax'
import jsonp from 'jsonp'
const net = ''

/* 登录接口 */
export const reqLogin = (username,password) => ajax(net+'/login',{username,password},'POST')
/* 获取天气的接口 */
export const getWeather = (city)=>{
    return new Promise((resolve,rejecct)=>{
        const url = `http://api.map.baidu.com/telematics/v3/weather?location=${city}&output=json&ak=3p49MVra6urFRGOT9s8UBWr2`
        jsonp(url,{},(err,data)=>{
            if(!err && data.status==='success'){
                const{dayPictureUrl,weather}=data.results[0].weather_data[0]
                resolve({dayPictureUrl,weather})
            }else{
                alert('Failed to get weather')
            }
        })
    })
}
/* 获取分类列表的接口 */
export const reqCategories = (parentId='0')=>ajax('/manage/category/list',{parentId})
/* 添加分类的接口 */
export const reqAddCategory = ({categoryName,parentId})=>ajax('/manage/category/add',{categoryName,parentId},'POST')
/* 更新分类的接口 */
export const reqModifyCategory = ({categoryId,categoryName})=> ajax('/manage/category/update',{categoryId,categoryName},'POST')
/* 根据分类ID获取分类的接口 */
export const reqGetCategory = (categoryId)=>ajax('/manage/category/info',{categoryId})
/* 一般获取商品分页列表的接口 */
export const reqProducts = (pageNum,pageSize)=>ajax('/manage/product/search',{pageNum,pageSize})
/* 搜索获取商品分页列表的接口 */
export const reqSearchProducts = ({pageNum,pageSize,searchType,searchName})=> ajax('/manage/product/search',{
  pageNum,
  pageSize,
  [searchType]:searchName
})
/* 点击切换销售状态的接口 */
export const reqUpdateProductStatus = ({productId,status})=> ajax('/manage/product/updateStatus',{productId,status},'POST')
/* 添加商品的接口 */
export const reqAddaProduct = (product)=> ajax('/manage/product/add',product,'POST')
/* 删除数据库里图片的接口 */
export const reqDeleteImg = (name)=> ajax('/manage/img/delete',{name},'POST')



